class Mythread{
    public static int  i = 0;
    public Object loker = new Object();

    public void add(){
        synchronized (this){
            while (i < 10) {
                System.out.print(i);
                i++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public synchronized void print(){
        System.out.print("哈哈哈");
    }
    public int  getInstance() {
        return i;
    }
}
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        Mythread mythread = new Mythread();
        Thread thread1 = new Thread(()->{
            int i = 0;
           while (i < 5){
               synchronized (mythread) {
                   mythread.add();
                   i++;
               }
                   //System.out.println("线程1:"+i);
           }
        });
        Thread thread2 = new Thread(()->{
            int i = 0;
                mythread.print();
                //System.out.println("线程2:"+i);
        });
        thread1.start();
        thread2.start();
    }
}
